var apiHost = wx.getStorageSync('apiHost');
var apiUpLoad = wx.getStorageSync('apiUpLoad');        //上传外网图片地址

Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiArray: [],                       //初始化时间集合
    deliveryMethodName: '请选择发货方式',  //配送方式名称
    deliveryMethod :'',                   //配送方式id
    multiIndex: [0, 0, 0],                //三级联动配送时间
    showCollageTime : ''                  //展示选择时间
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    var deliveryMethodFlag = options.deliveryMethod;

    var deliveryMethodName = '';

    //判断发货方式参数
    if (deliveryMethodFlag != undefined){
       deliveryMethodName =  that.judgeDeliveryMethod(deliveryMethodFlag); //判断发货方式
      //发货方式
      that.setData({
        deliveryMethod: deliveryMethodFlag,
        deliveryMethodName: deliveryMethodName
      })
      wx.setStorageSync('deliveryMethodName', deliveryMethodName);
    }else{
      var deliveryMethod = wx.getStorageSync('deliveryMethod');
      deliveryMethodName = wx.getStorageSync('deliveryMethodName');
      that.setData({
        deliveryMethod: deliveryMethod,
        deliveryMethodName: deliveryMethodName
      })
    }

    that.initCollageTime();//初始化成团时间

    var collageId = wx.getStorageSync('collageId');                     //拼团id
    var originalPrice = wx.getStorageSync('originalPrice');             //原价
    var collagePrice = wx.getStorageSync('collagePrice');               //拼团价格
    var peopleCount = wx.getStorageSync('peopleCount');                 //拼团人数
    var showCollageTime = wx.getStorageSync('showCollageTime');         //成团时间

    if (deliveryMethodName == ""){
      deliveryMethodName = '请选择发货方式';
    }

    that.setData({
      originalPrice: originalPrice,
      collagePrice: collagePrice,
      peopleCount: peopleCount,
      showCollageTime: showCollageTime
    })
  },

  /**
   * 原价  输入框
   */
  originalPriceInput:function(e){
    var originalPrice = e.detail.value;
    wx.setStorageSync('originalPrice', originalPrice);
    console.log(originalPrice)
    this.setData({
      originalPrice: originalPrice
    })
  },
  /**
   * 拼团价  输入框
   */
  collagePriceInput: function (e) {
    var collagePrice = e.detail.value;
    wx.setStorageSync('collagePrice', collagePrice);
    this.setData({
      collagePrice: collagePrice
    })
  },

  /**
   * 拼团人数  输入框
   */
  peopleCountInput: function (e) {
    var peopleCount = e.detail.value;

    this.setData({
      peopleCount: peopleCount
    })
    wx.setStorageSync('peopleCount', peopleCount);
  },

  /**
   * 初始化成团时间
   */
  initCollageTime : function(){
    var that = this;
    var day = 7;            //天
    var hour = 24;          //小时
    var minute = 60;        //分钟
    var dayList = [];
    var hourList = [];
    var minuteList = [];
    var multiArrayList = [];//总的集合

    for (var a = 0; a < day; a++) {
      dayList.push(a + " 天 ")
    }

    for (var b = 0; b < hour; b++) {
      hourList.push(b + " 小时 ")
    }

    for (var c = 0; c < minute; c++) {
      minuteList.push(c + " 分钟 ")
    }

    multiArrayList.push(dayList)
    multiArrayList.push(hourList)
    multiArrayList.push(minuteList)

    that.setData({
      multiArray: multiArrayList
    })
  },

  /**
   * 选择成团时间 确认方法
   */
  bindMultiPickerChange: function (e) {
    var that = this;
    var onChangeList = e.detail.value;    //选择数据集合
    that.PickerChange(onChangeList);
  },
  /**
   * 选择成团时间通用方法
   */
  PickerChange: function (onChangeList) {
    var that = this;
    var onChangeList = onChangeList;    //选择数据集合
    var multiArray = that.data.multiArray;//初始化集合

    var changeDay = onChangeList[0];    //选择天
    var changeHour = onChangeList[1];   //小时
    var changeMinute = onChangeList[2]; //分钟

    var changeDayValue = multiArray[0];    //天
    var changeHourValue = multiArray[1];   //小时
    var changeMinuteValue = multiArray[2]; //分钟

    var showCollageTime = '';           //展示给用户看的成团时间

    if (changeDay != 0) {
      showCollageTime += changeDayValue[changeDay]
    }

    if (changeHour != 0) {
      showCollageTime += changeHourValue[changeHour]
    }

    if (changeMinute != 0) {
      showCollageTime += changeMinuteValue[changeMinute]
    }

    that.setData({
      showCollageTime: showCollageTime
    });
    wx.setStorageSync('showCollageTime', showCollageTime);
  },

  /**
  * 判断发货方式
  */
  judgeDeliveryMethod: function (deliveryMethod) {

    switch (deliveryMethod) {
      case 'delivery':
        return '配送发货';
        break;
      case 'self':
        return '自有自提点';
        break;
      case 'qcode':
        return '二维码凭证';
        break;
      case 'virtualCode':
        return '虚拟码';
        break;
      default:
        return '出错了';
        break;
    }
  },

  /**
   * 发货方式
   */
  goDelivery: function () {
    var that = this;

    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });

    var deliveryMethodFlag = that.data.deliveryMethod;      //发货方式
    var originalPrice = that.data.originalPrice;            //原价
    var collagePrice = that.data.collagePrice;              //拼团价
    var peopleCount = that.data.peopleCount;                //拼团人数
    var showCollageTime = that.data.showCollageTime;        //成团时间


    // 设置为全局变量
    wx.setStorageSync('originalPrice', originalPrice);
    wx.setStorageSync('collagePrice', collagePrice);
    wx.setStorageSync('peopleCount', peopleCount);
    wx.setStorageSync('showCollageTime', showCollageTime);

    wx.reLaunch({
      url: '../delivery/delivery?deliveryMethodFlag=' + deliveryMethodFlag,
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 跳转高级设置
   */
  goSeniorSetup:function(){
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });
    wx.reLaunch({
      url: '../seniorSetUp/seniorSetUp',
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 上一步
   */
  theLastStep : function(){
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });
    wx.reLaunch({
      url: '../firstOrdinaryCollage/firstOrdinaryCollage',
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 下一步
   */
  theNextStep:function(){
    wx.showLoading({
      title: '跳转中',
      icon: 'loading',
      mask: true
    });
    wx.reLaunch({
      url: '../detail/detail',
      success: function () {
        wx.hideLoading();   //隐藏加载loading
      },
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  }
})